var api = require('../../config/api.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    psCustomerCode: '',
    footer: {},
    feedbackStatisticsInfo: {
      'handledNum': 0,
      'observeNum': 0,
      'pendingNum': 0,
      'rejectNum': 0,
    },
    workOrderStatisticsInfo: {
      'waitNum': 0,
      'handledNum': 0,
      'receiveNum': 0,
      'reachNum': 0,
      'detectionNum': 0,
      'maintenanceNum': 0,
      'endNum': 0,
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getfooter()
    this.doHttpGetFeedbackStatistics()
    this.doHttpGetWorkOrderStatistics()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.popup = this.selectComponent("#popup");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.doHttpGetFeedbackStatistics()
    this.doHttpGetWorkOrderStatistics()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  /**
   * 初始化底部Tab数据
   */
  getfooter: function () {
    var that = this
    wx.getStorage({
        key: 'psCustomerCode',
        success: (RES) => {
            that.setData({
                customerCode: RES.data
            });
            console.log('customerCode为', RES.data)
            wx.request({
                url: api.findUnread,
                method: 'POST',
                data: {
                    psCustomerCode: that.data.customerCode,
                },
                success: function (res) {
                    if (res.data.code == 200) {
                        that.setData({
                            unreadNum: res.data.data.unreadNum,
                        }),
                        console.log("未读消息数量:", that.data.unreadNum)
                        // var t = this;
                        var footerList = {
                            footerTab: 3,
                            footerlist: [{
                                diy_name: "设备概览",
                                color: '#221915',
                                img: "",
                                selimg: "",
                                link: "/pages/index/index"
                            }, {
                                diy_name: "实时监测",
                                color: '#221915',
                                img: "",
                                selimg: "",
                                link: "/pages/indexMonitor/indexMonitor",
                                unreadNum: that.data.unreadNum
                            }, {
                                diy_name: "",
                                color: '#221915',
                                img: "https://www.cqwuyang.cn/images/Recognition.png",
                                selimg: "https://www.cqwuyang.cn/images/Recognition.png",
                                link: ""
                            }, {
                                diy_name: "上报总览",
                                color: '#221915',
                                img: "",
                                selimg: "",
                                link: "/pages/indexSummary/indexSummary"
                            }, {
                                diy_name: "个人中心",
                                color: '#221915',
                                img: "",
                                selimg: "",
                                link: "/pages/indexMy/indexMy"
                            }]
                        }
                        that.setData({
                            footer: footerList
                        })
                    }

                }
            })
        },
    });



},

  /**
   * 获取上报数据统计
   */
  doHttpGetFeedbackStatistics: function () {
    var that = this;
    wx.getStorage({
        key: 'psCustomerCode',
        success: (res) => {
          that.setData({
            psCustomerCode: res.data
          });    
          wx.request({
            url: api.getFeedbackStatistics,
            method: 'POST',
            data: {
              psCustomerCode: that.data.psCustomerCode,
            },
            success: function (res) {
              if (res.data.code == 200) {
                that.setData({
                  feedbackStatisticsInfo: res.data.data,
                });
              }
            }
          })
        },
      });
    
  },

  /**
   * 获取上报数据统计
   */
  doHttpGetWorkOrderStatistics: function () {
    var that = this
    wx.getStorage({
        key: 'psCustomerCode',
        success: (res) => {
          that.setData({
            psCustomerCode: res.data
          });    
          wx.request({
            url: api.getWorkOrderStatistics,
            method: 'POST',
            data: {
              customerCode: that.data.psCustomerCode,
            },
            success: function (res) {
              if (res.data.code == 200) {
                that.setData({
                  workOrderStatisticsInfo: res.data.data,
                });
              }
            }
          })
        },
      });
   
  },

  changeRange: function () {
    this.popup.changeRange();
  },

  /**
   * 跳转工单列表
   */
  toWorkOrderList(e) {
    wx.navigateTo({
      url: "/pages/workOrderList/workOrderList?workOrderState=" + e.currentTarget.dataset.state
    });
  },

  /**
   * 跳转上报列表
   */
  toFeedbackList(e) {
    wx.navigateTo({
      url: "/pages/feedbackList/feedbackList?feedBackState=" + e.currentTarget.dataset.state
    });
  },
})